n, m = tuple(map(int, input().split()))
u = []
v = []
roads = []

for i in range(int(m)):
    a, b = tuple(map(int, input().split()))
    u.append(a)
    v.append(b)
    roads.append((a, b))

k = int(input())

x = []
y = []
meetings = []
for i in range(int(k)):
    c, d = tuple(map(int, input().split()))
    x.append(c)
    y.append(d)
    meetings.append((c, d))

thoughts = dict()
for meeting in meetings:
    thought = thoughts.get(str(meeting[0]), [meeting[0]])
    thought.append(meeting[1])
    thoughts[str(meeting[0])] = thought

    thought = thoughts.get(str(meeting[1]), [meeting[0]])
    thought.append(meeting[0])
    thoughts[str(meeting[1])] = thought

for thought in thoughts:
    print(set(thought))


print(thoughts)
